<?php
include('SocuteTemplateSection.php');
include('ISocuteTemplateWriter.php');
include('SocuteTemplateDefaultWriter.php');

class SocuteTemplate
{
	public $templateEscapeChar = '\\';
	
	public $valueOpenTag = '[$';
	public $valueCloseTag = ']';
	
	public $sectionOpenTag = '{$';
	public $sectionCloseTag = '}';
	public $sectionStartTag = ':';
	
	public $rootSection = null;
  public $defaultWriter = null;
  
  private $filename = '';
  private $contents = '';
  
  public function __construct($filename='')
  {
    if(strlen($filename) > 0){
      $this->load($filename);
    }
  }
  
	public function load($filename)
  {
    $this->filename = $filename;
    $this->init($filename);
  }
  
  public function init($name)
	{
		$this->rootSection = $this->makeSection($name);
    $this->defaultWriter = new SocuteTemplateDefaultWriter();
    $this->validateTemplate();
	}
	
	public function set($name, $value){
		$this->rootSection->set($name, $value);
	}
	
	public function dispatch($section){
		$this->rootSection->dispatch($section);
	}
	
	public function makeSection($sectionName)
	{
		return new SocuteTemplateSection($sectionName, $this);
	}
	
	public function render()
	{
    if(strlen($this->contents) == 0){
      if(strlen($this->filename) > 0){
        $this->contents = file_get_contents($this->filename);
      }
    }
    
		$this->rootSection->render($this->contents, $this->defaultWriter);
	}
  
  public function validateTemplate()
	{
		// to be determined
	}
}
?>